package chatRoom.com.fenwe.util.annotation.resolve;

import java.lang.reflect.Field;

import org.springframework.util.StringUtils;

import chatRoom.com.fenwe.constant.Constant;
import chatRoom.com.fenwe.util.annotation.type.Column;
import chatRoom.com.fenwe.util.exception.ParameterValidateException;

/**
 * 
* 项目名称:  聊天室
* 包:        chatRoom.com.fenwe.util.annotation.resolve 
* 类名称:    AnnotationResolveUtil.java
* 类描述:    注解解析工具类
* 创建人:    fw  
* 创建时间:  2017年7月14日 上午10:26:18
 */
public class AnnotationResolveUtil {
    public static void resolveCommonClass(Object object) throws IllegalArgumentException, IllegalAccessException {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Column column = field.getAnnotation(Column.class);
            if (column != null) {
                if (Constant.LogicVariable.NO_N.equals(column.isNull())) {
                    Class<?> returnType = field.getType();
                    if (returnType == String.class && StringUtils.isEmpty(field.get(object))) {
                        throw new ParameterValidateException(column.name() + "不能为空！");
                    } else if (null == field.get(object)) {
                        throw new ParameterValidateException(column.name() + "不能为空！");
                    }
                }
            }
        }

    }

}
